/**
 * 将万亿以下的数字转为中文
 * @param {number} num 阿拉伯数字
 * @returns {string} 中文数字
 */
function toChineseNumber(num) {
  const numStr = num
    .toString()
    .replace(/(?=(\d{4})+$)/g, ",")
    .split(",")
    .filter(Boolean);
  const chars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  const units = ["", "十", "百", "千"];
  function handleZero(str) {
    return str.replace(/零{2,}/g, "零").replace(/零+$/g, "");
  }
  function _transform(n) {
    if (n === "0000") {
      return chars[0];
    }
    let result = "";
    for (let i = 0; i < n.length; i++) {
      const c = chars[+n[i]];
      let u = units[n.length - i - 1];
      if (c === chars[0]) {
        u = "";
      }
      result += c + u;
    }
    result = handleZero(result);
    return result;
  }

  const bigUnits = ["", "万", "亿"];
  let result = "";
  for (let i = 0; i < numStr.length; i++) {
    const part = numStr[i];
    const c = _transform(part);
    let u = bigUnits[numStr.length - i - 1];
    if (c === chars[0]) {
      u = "";
    }
    result += c + u;
  }
  result = handleZero(result);
  return result;
}

// 测试
const result = toChineseNumber(11);
console.log(result);
